package com.zzyl.nursing.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.AlertDataQueryDto;
import com.zzyl.system.service.ISysRoleService;
import lombok.extern.slf4j.Slf4j;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.dto.HandleAlertDataDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-25
 */
@Service
@Slf4j
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param dto 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertDataQueryDto dto)
    {
        //获取当前登陆人id
        Long userId = SecurityUtils.getUserId();
        dto.setUserId(userId);
        //拿着当前登陆人id查询报警数据表,(具体为哪个字段?)
        return alertDataMapper.selectAlertDataList(dto);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 处理设备报警数据
     *
     * @param dto
     */
//    @Transactional(rollbackFor = Exception.class)
    @Override
    public int handleAlertData(HandleAlertDataDto dto) {
        //修改数据
        if (dto.getId() == null) {
            throw new IllegalArgumentException("报警数据ID不能为空");
        }

        AlertData one = lambdaQuery()
                .eq(AlertData::getId, dto.getId())
                .eq(AlertData::getStatus, 0)
                .one();

        if (one == null) {
            throw new RuntimeException("数据不存在");
        }
        one.setProcessingResult(dto.getProcessingResult());
        one.setProcessingTime(dto.getProcessingTime());
        one.setStatus(1);
        //通过 SecurityUtils.getUserId() 获取当前登录用户 ID 和用户名
        try {
            Long currentUserId = SecurityUtils.getUserId();
            String username = SecurityUtils.getUsername();
            one.setProcessorId(currentUserId);
            one.setProcessorName(username);
        } catch (Exception e) {
            throw new RuntimeException("获取用户信息失败");
        }
     LambdaQueryWrapper<AlertData> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(AlertData::getIotId, one.getIotId())
                .eq(AlertData::getFunctionId, one.getFunctionId())
                .eq(AlertData::getDataValue, one.getDataValue())
                .eq(AlertData::getAlertRuleId, one.getAlertRuleId())
                .eq(AlertData::getCreateTime, one.getCreateTime());
        return update(one,wrapper) ? 1 : 0;
    }

}
